package org.study.local.boot.controller;

import com.google.common.collect.Maps;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.study.local.boot.service.LocalFileService;

import java.util.Map;

/**
 * 作用描述
 *
 * @author doveylovey
 * @version v1.0.0
 * @email 1135782208@qq.com
 * @date 2023年12月13日
 */
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@RestController
@RequestMapping("/local")
public class LocalFileController {
    private final LocalFileService localFileService;

    @PostMapping("/upload")
    public ResponseEntity<Map<String, Object>> uploadFile(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            Map<String, Object> retMap = Maps.newLinkedHashMap();
            retMap.put("code", "9999");
            retMap.put("msg", "请选择一个文件上传");
            return new ResponseEntity<>(retMap, HttpStatus.BAD_REQUEST);
        }
        return new ResponseEntity<>(localFileService.upload(file), HttpStatus.BAD_REQUEST);
    }
}
